package com.mes.controller.manu;


import com.mes.entity.User;
import com.mes.service.manu.AbnormalReportService;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 人工针车 -- 异常上报
 */
@Controller
@RequestMapping("abnormalReport")
public class AbnormalReportController {

    @Autowired
    private AbnormalReportService abnormalReportService;

    /**
     * 获取异常原因列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value="getList", method= RequestMethod.POST)
    public Message getList() {
        Message msg = new Message();
        // 获取异常原因列表
        msg.setData("exceptionList", abnormalReportService.getExceptionList());
        // 获取所有设备列表
        msg.setData("deviceList", abnormalReportService.getDeviceList());
        // 获取用户列表
        msg.setData("userList", abnormalReportService.getUserList());
        return msg;
    }

    /**
     * 新增异常上报记录
     * @return
     */
    @ResponseBody
    @RequestMapping(value="addExceptionLog", method= RequestMethod.POST)
    public Message addExceptionLog(HttpServletRequest request) {
        Message msg = new Message();
        String exception = request.getParameter("exception");
        String facilites = request.getParameter("facilites");
        String receiver = request.getParameter("receiver");
        String description = request.getParameter("description");
        User user = UserUtil.getLoginUser(request);

        try {
            abnormalReportService.addExceptionLog(exception, facilites, receiver, description, user);
            msg.setOkMessage("异常上报成功！");
        } catch (Exception e) {
            msg.setErrorMessage("异常上报失败！");
            e.printStackTrace();
        }

        return msg;
    }

}
